package com.itqf.Controller;

import com.itqf.Service.Ordertopic.OrderTopicService;
import com.itqf.Service.Shippingaddress.ShippingaddressService;
import com.itqf.Service.Topic.TopicService;
import com.itqf.Service.User.UserService;
import com.itqf.Util.JwtUtils;
import com.itqf.entity.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.UUID;

@Controller
public class TopicpayforController {
    @Resource
    private ShippingaddressService shippingaddressService;
    @Resource
    private TopicService topicService;
    @Resource
    private UserService userService;
    @Resource
    private OrderTopicService orderTopicService;

    //显示收货地址，默认第一个收货地址
    @RequestMapping(value = "/neworder",method = RequestMethod.GET)
    @ResponseBody
    public TopicPayforJson getshippingaddress(
     int topicid, int code, HttpServletRequest request)
    {  JwtUtils jwtUtils=new JwtUtils();

        User user = jwtUtils.Soluecookie(request);
        /*User user=new User();
        user.setUserid("1");*/
        String userid = user.getUserid();
        //查询用户是不是会员
        User user1 = userService.findusers(userid);
        TopicPayforJson topicPayforJson=new TopicPayforJson();
        if(user1==null)
        {
            topicPayforJson.setMessage("用户没有登录，请登录");
            return topicPayforJson;
        }
        else {
            Integer userstate = user1.getUserstate();

            //根据userid查找所有收货信息
            List<Shippingaddress> list=shippingaddressService.findaddress(userid);
            if(list.isEmpty())
            {
                //对应用户没有收货信息，发送一个消息。
                topicPayforJson.setMessage("您没有收件人，请添加一个收件人");
                return topicPayforJson;
            }else {
               /* topicPayforJson.setShippingaddressList(list);*/
                //默认选择第一个收件地址
                /*if(shippingid==1)
                {*/
                    Shippingaddress shippingaddress = list.get(0);
                    topicPayforJson.setShippingaddress(shippingaddress);
              /*  }
                else {
                    Shippingaddress shippingaddress = shippingaddressService.selectByPrimaryKey(shippingid);
                    topicPayforJson.setShippingaddress(shippingaddress);
                }*/
                //回显购买选择的商品
                Topic topic=topicService.findtopbytopicid(topicid);
                topicPayforJson.setTopic(topic);
                Ordertopic ordertopic=new Ordertopic();
                //添加订单数量，修改订单为待支付状态,显示订单总价
                if(code==0)
                {
                    HttpSession session = request.getSession();
                    if(userstate==1)
                    {
                        ordertopic.setTopicInt(1);
                        ordertopic.setTopicState(1);
                        ordertopic.setTopicTotalamount(topic.getVipPrice());
                        ordertopic.setTopicid(topicid);
                        ordertopic.setOrderaddressid(shippingaddress.getShippingid());
                        UUID uuid = UUID.randomUUID();
                        String ordertopicid = uuid.toString().replace("-","");
                        ordertopic.setOrdertopicid(ordertopicid);
                        orderTopicService.insertordertopic(ordertopic);
                        session.setAttribute("orderid",ordertopicid);
                        session.setAttribute("vipprice",topic.getVipPrice());
                        session.setAttribute("userstate",userstate);
                        //orderTopicService.updateordertopic(ordertopic.getOrderid(),(topic.getVipPrice()*ordertopic.getTopicInt()));
                        Ordertopic ordertopic1 =orderTopicService.findordertopic(ordertopic.getOrdertopicid());
                        topicPayforJson.setOrdertopic(ordertopic1);
                        Double topicTotalamount = ordertopic1.getTopicTotalamount();
                        topicPayforJson.setTopicTotalamount(topicTotalamount);
                    }
                    if(userstate==0)
                    {
                        ordertopic.setTopicInt(1);
                        ordertopic.setTopicState(1);
                        ordertopic.setTopicTotalamount(topic.getPrice());
                        ordertopic.setTopicid(topicid);
                        UUID uuid = UUID.randomUUID();
                        String ordertopicid = uuid.toString().replace("-","");
                        ordertopic.setOrdertopicid(ordertopicid);
                        ordertopic.setOrderaddressid(shippingaddress.getShippingid());
                        orderTopicService.insertordertopic(ordertopic);
                        session.setAttribute("orderid",ordertopicid);
                        session.setAttribute("price",topic.getPrice());
                        session.setAttribute("userstate",userstate);
                        //orderTopicService.updateordertopic(ordertopic.getOrderid(),(topic.getVipPrice()*ordertopic.getTopicInt()));
                        Ordertopic ordertopic1 =orderTopicService.findordertopic(ordertopic.getOrdertopicid());
                        topicPayforJson.setOrdertopic(ordertopic1);
                        Double topicTotalamount = ordertopic1.getTopicTotalamount();
                        topicPayforJson.setTopicTotalamount(topicTotalamount);

                    }
                }
                return topicPayforJson;
            }
        }
    }
    //修改购买商品的数量
    @RequestMapping(value = "/topicInt",method = RequestMethod.POST)
    @ResponseBody
    public Double updatetopicint(@RequestParam(defaultValue = "1") int topicInt,HttpServletRequest request)
    {
        HttpSession session = request.getSession();
               int userstate =(int)session.getAttribute("userstate");
        if(userstate==1)
        {
            String orderid1 = (String)session.getAttribute("orderid");
            double vipprice=(double)session.getAttribute("vipprice");
            orderTopicService.updateordertopic(orderid1,topicInt,(vipprice*topicInt));
            Ordertopic ordertopic1 =orderTopicService.findordertopic(orderid1);
            Double topicTotalamount = ordertopic1.getTopicTotalamount();
            return topicTotalamount;
        }
        else
        {
            String orderid1 = (String)session.getAttribute("orderid");
            double price=(double)session.getAttribute("price");
            orderTopicService.updateordertopic(orderid1,topicInt,(price*topicInt));
            Ordertopic ordertopic1 =orderTopicService.findordertopic(orderid1);
            Double topicTotalamount = ordertopic1.getTopicTotalamount();
            return topicTotalamount;
        }
    }
    //查找所有的收件人的信息
    @RequestMapping(value = "/shippingaddress",method = RequestMethod.GET)
    @ResponseBody
    public List getshippingaddress(HttpServletRequest request)
    {
        JwtUtils jwtUtils=new JwtUtils();
        User user = jwtUtils.Soluecookie(request);
        /*User user=new User();
        user.setUserid("1");*/
        String userid = user.getUserid();
        List<Shippingaddress> list=shippingaddressService.findaddress(userid);
        return list;
    }
    //选择收件人的信息
    @RequestMapping(value = "/choiceaddress",method = RequestMethod.GET)
    @ResponseBody
    public Shippingaddress getaddress(int shippingid,HttpServletRequest request)
    {
        HttpSession session = request.getSession();
        String orderid1 = (String)session.getAttribute("orderid");
            orderTopicService.updateordertopicbyaddress(orderid1,shippingid);
            Ordertopic ordertopic1 =orderTopicService.findordertopic(orderid1);
            Integer orderaddressid = ordertopic1.getOrderaddressid();
            Shippingaddress shippingaddress1 = shippingaddressService.selectByPrimaryKey(orderaddressid);
            return shippingaddress1;
    }
    //支付完成，增加order与user的关系
    @RequestMapping(value = "/payfor",method = RequestMethod.POST)
    @ResponseBody
    public int payfor(int code,String ordertopicid,HttpServletRequest request)
    {
        JwtUtils jwtUtils=new JwtUtils();
        User user = jwtUtils.Soluecookie(request);
        /*User user=new User();
        user.setUserid("1");*/
        String userid = user.getUserid();
        Ordertopic ordertopic = orderTopicService.findordertopic(ordertopicid);
        if(ordertopic.getTopicState()==1&&code==1) {
            int paycode = orderTopicService.updatebyordertopicid(ordertopicid, userid);
            return paycode;
        }
        return 404;
    }
}
